<template>
  <section class="component warehousing-vehicle-report">
    <data-form :model="dataForm" @onSearch="refreshData" :page="pageService">
      <template slot="inputs">
        <el-form-item label="订单号" prop="caseNumber">
          <el-input v-model="dataForm.caseNumber"></el-input>
        </el-form-item>
        <el-form-item label="批次号" prop="batchNumber">
          <el-input v-model="dataForm.batchNumber"></el-input>
        </el-form-item>
        <el-form-item label="委托方" prop="principalId">
          <el-select v-model="dataForm.principalId" placeholder="请选择" clearable>
            <el-option v-for="{index,name,id} in principalList.content" :key="index" :label="name" :value="id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="放款日期" prop="loanDate">
          <el-date-picker type="daterange" :editable="false" v-model="dataForm.loanDate" placeholder="选择日期时间" clearable>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="上牌地" prop="boardAddress">
          <el-input v-model="dataForm.boardAddress"></el-input>
        </el-form-item>
        <el-form-item label="业务团队" prop="partnerName">
          <el-input v-model="dataForm.partnerName"></el-input>
        </el-form-item>
        <el-form-item label="贷款银行" prop="bankName">
          <el-input v-model="dataForm.bankName"></el-input>
        </el-form-item>
        <el-form-item label="业务承接地" prop="businessAddress">
          <el-input v-model="dataForm.businessAddress"></el-input>
        </el-form-item>
        <el-form-item label="车库" prop="garageId">
          <el-select v-model="dataForm.garageId" placeholder="请选择" clearable>
            <el-option v-for="{index,garageName,id} in garageList" :key="index" :label="garageName" :value="id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属机构" prop="deptId">
          <select-department v-model="dataForm.deptId" @on-select-change="(id, code) => departemntChange(id, code)"></select-department>
        </el-form-item>
        <el-form-item label="入库申请人" prop="applyRealName">
          <el-input v-model="dataForm.applyRealName"></el-input>
        </el-form-item>
        <el-form-item label="入库审批人" prop="approvalRealName">
          <el-input v-model="dataForm.approvalRealName"></el-input>
        </el-form-item>
        <el-form-item label="入库时间" prop="inWarehouseTime">
          <el-date-picker type="datetimerange" :editable="false" v-model="dataForm.inWarehouseTime" placeholder="选择日期时间" clearable></el-date-picker>
        </el-form-item>
        <el-form-item label="车辆状态" prop="warehouseCarStatus">
          <el-select v-model="dataForm.warehouseCarStatus" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('WarehouseCarStatus')" :key="index" :label="name" :value="code"></el-option>
          </el-select>
        </el-form-item>
      </template>
    </data-form>

    <data-box :data="dataSet" @onPageChange="refreshData" :page="pageService">
      <template slot="buttons">
        <el-button @click="onClickExport" v-auth="2107">导出</el-button>
      </template>
      <template slot="columns">
        <el-table-column prop="caseNumber" label="订单编号" :min-width="$helper.getColumnWidth(6)">
          <template slot-scope="scope">
            <el-button type="text" @click="openExecutionPage(scope.row)">{{scope.row.caseNumber}}</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="batchNumber" label="批次号" :min-width="$helper.getColumnWidth(5)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="principalName" label="委托方" :min-width="$helper.getColumnWidth(5)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="personalName" label="客户姓名" :min-width="$helper.getColumnWidth(5)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="idCard" label="身份证号" :min-width="$helper.getColumnWidth(5)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="loanDate" label="放款日期" :formatter="(row) => $filter.dateFormat(row.loanDate)" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="mobileNo" label="手机号" :min-width="$helper.getColumnWidth(4)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="boardAddress" label="上牌地" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="plateNum" label="车牌号" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="partnerName" label="业务团队" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="bankName" label="贷款银行" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="overdueAmount" label="催收总金额" :min-width="$helper.getColumnWidth(6)" :formatter="(row) =>  $filter.toThousands(row.overdueAmount)">
        </el-table-column>
        <el-table-column prop="businessAddress" label="业务承接地" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="garageName" label="车库" :min-width="$helper.getColumnWidth(5)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="deptName" label="所属机构" :min-width="$helper.getColumnWidth(5)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="applyRealName" label="入库申请人" :min-width="$helper.getColumnWidth(5)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="approvalRealName" label="入库审批人" :min-width="$helper.getColumnWidth(5)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="inWarehouseTime" label="入库时间" :min-width="$helper.getColumnWidth(5)" show-overflow-tooltip :formatter="(row) => $filter.dateTimeFormat(row.inWarehouseTime)">
        </el-table-column>
        <el-table-column prop="warehouseCarStatus" label="车辆状态" :formatter="(row) => $filter.dictConvert(row.warehouseCarStatus,'WarehouseCarStatus')" :min-width="$helper.getColumnWidth(5)" show-overflow-tooltip>
        </el-table-column>
      </template>
    </data-box>
  </section>
</template>
<script lang="ts">
import { Vue, Component } from 'vue-property-decorator'
import DataBox from '~/components/common/data-box.vue'
import DataForm from '~/components/common/data-form.vue'
import NumberRange from '~/components/common/number-range.vue';
import { Auth, Layout, Dependencies } from '~/core/decorator';
import { PageService } from '~/utils/page.service'
import { State } from "vuex-class";
import { CarManageService } from '~/services/domain-service/car-manage.service'
import SelectDepartment from "~/components/business-component/select-department.vue";
import { CASE_CHANNEL, MENU_CHANNEL } from "~/config/enum.config";

@Auth(2103)
@Layout('workspace')
@Component({
  components: {
    DataBox,
    DataForm,
    NumberRange,
    SelectDepartment
  }
})
export default class WarehousingVehicleReport extends Vue {
  @Dependencies(PageService)
  private pageService: PageService;
  @Dependencies(CarManageService) private carManageService: CarManageService

  @State private principalList: any;

  private dataForm: any = {
    batchNumber: "",
    caseNumber: '',
    principalId: '',
    partnerName: '',
    bankName: '',
    businessAddress: '',
    boardAddress: '',
    loanDate: [],
    inWarehouseTime: [],
    applyRealName: '',
    warehouseCarStatus: '',
    garageId: '',
    deptId: '',
    approvalRealName: ''
  };
  private dataSet: any = []
  private garageList: any = []

  private refreshData() {
    this.carManageService.getWarehouseCarReport(this.paramsHandler(), this.pageService).subscribe(data => {
      this.dataSet = data
    })
  }

  private onClickExport() {
    if (!this.dataSet.length) return this.$message.warning('暂无数据无需导出！')
    this.carManageService.warehouseCarReportExport(this.paramsHandler()).subscribe(data => {
      this.$common.downloadFile(data.url)
    })
  }

  private paramsHandler() {
    const params = { ...this.dataForm }
    params.loanDateMin = this.$filter.dateRanageFormat(this.dataForm.loanDate).start
    params.loanDateMax = this.$filter.dateRanageFormat(this.dataForm.loanDate).end
    params.inWarehouseTimeMin = this.dataForm.inWarehouseTime.length ? this.$filter.dateTimeFormat(this.dataForm.inWarehouseTime[0]) : ''
    params.inWarehouseTimeMax = this.dataForm.inWarehouseTime.length ? this.$filter.dateTimeFormat(this.dataForm.inWarehouseTime[1]) : ''

    delete params.applyTime
    delete params.loanDate
    return params
  }

  private departemntChange(id, code) { }

  private getGarageList() {
    this.carManageService.getAllGaragesList().subscribe(data => {
      this.garageList = data
    })
  }

  private created() {
    this.refreshData()
    this.getGarageList()
  }

  /**
   * 进入查看执行页
   */
  private openExecutionPage(row) {
    this.$router.openCaseDetail(row.id, {
      menuChannel: MENU_CHANNEL.Warehousing_vehicle_report,
      hiddenCaseList: true,
      hiddenAction: true
    });
  }
}
</script>
